package com.example.demo.gateway.support;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @Auther: miaoguoxin
 * @Date: 2020/4/15 20:33
 * @Description:
 */
@Component
public class ReactiveRequestBeanProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        beanFactory.registerResolvableDependency(ServerHttpRequest.class,new ReactiveRequestObjectFactory());
    }


    private static class ReactiveRequestObjectFactory implements ObjectFactory<ServerHttpRequest>, Serializable{

        @Override
        public ServerHttpRequest getObject() throws BeansException {
            return ReactiveRequestContextHolder.get();
        }
    }

}
